package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.pojo.Account;
import com.bdqn.pojo.Comment;
import com.bdqn.pojo.RoomType;
import com.bdqn.service.AccountService;
import com.bdqn.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {
    @Resource
    CommentService commentService;
    @Resource
    private AccountService accountService;

    /**
     * 添加评论
     * @param model
     * @param content
     * @param roomTypeId
     * @param request
     * @return
     */
//    @RequestMapping("comment/add")
//    public String add(Model model,String content,Long roomTypeId,HttpServletRequest request){
//        Account account = (Account) request.getSession().getAttribute("account");
//        if(account==null){
//            System.out.println("请输入登录");
//            return "redirect:/home/login2";
//
//        }else{
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
//            Comment comment=new Comment();
//            comment.setContent(content);
//            comment.setAccountId(account.getId());
//            comment.setRoomTypeId(roomTypeId);
//            comment.setTime(df.format(new Date()));
//            int aa=commentService.add(comment);
//            if(aa>0){
//                return "redirect:/ajax";
//            }
//        }
//
//       return "home/index/index";
//    }

    /**
     * 添加评论
     * @param content
     * @param roomTypeId
     * @return
//     */
//    @PostMapping("comment/add")
//    @ResponseBody
//    public String add(String content, Long roomTypeId, HttpSession session){
//        Map<String, String> result = new HashMap<>();
//        Account account = (Account) session.getAttribute("account");
//        if(account==null){
//            return "<script>window.location=\"/home/login2\"</script>";
//        }
//
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
//        Comment comment=new Comment();
//        comment.setContent(content);
//        comment.setAccountId(account.getId());
//        comment.setRoomTypeId(roomTypeId);
//        comment.setTime(df.format(new Date()));
//        int save = commentService.add(comment);
//
//        Comment newComment = comment;
//        newComment.setAccount(account);
//
//        String temp = "\t<div class=\"knsl-review-card knsl-scroll-animation\">\n" +
//                "\t\t<div class=\"knsl-card-header\">\n" +
//                "\t\t\t<div class=\"knsl-name-and-photo\">\n" +
//                "\t\t\t\t<div class=\"knsl-avatar-frame\">\n" +
//                "\t\t\t\t\t<img src=\"" + newComment.getAccount().getImg() + "\" alt=\"Avatar\">\n" +
//                "\t\t\t\t</div>\n" +
//                "\t\t\t\t<div>\n" +
//                "\t\t\t\t\t<h5>" + newComment.getAccount().getName() + "</h5>\n" +
//                "\t\t\t\t\t<span>" + newComment.getTime() + "</span>\n" +
//                "\t\t\t\t</div>\n" +
//                "\t\t\t</div>\n" +
//                "\t\t</div>\n" +
//                "\t\t<blockquote>\n" +
//                "\t\t   " + newComment.getContent() + "\n" +
//                "\t\t</blockquote>\n" +
//                "\t</div>\n";
//
//        result.put("result", temp);
//        return JSON.toJSONString(result);
//
//    }

    /**
     * 添加评论
     * @param content
     * @param roomTypeId
     * @return
     */
    @PostMapping("comment/add")
    @ResponseBody
    public String add(String content, Long roomTypeId, HttpSession session){
//        Map<String, String> result = new HashMap<>();
        Account account = (Account) session.getAttribute("account");
        if(account==null){
            return "<script>window.location=\"/home/login2\"</script>";
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        Comment comment=new Comment();
        comment.setContent(content);
        comment.setAccountId(account.getId());
        comment.setRoomTypeId(roomTypeId);
        comment.setTime(df.format(new Date()));
        int save = commentService.add(comment);

        Comment newComment = comment;
        newComment.setAccount(account);

        String temp = "\t<div class=\"knsl-review-card knsl-scroll-animation\">\n" +
                "\t\t<div class=\"knsl-card-header\">\n" +
                "\t\t\t<div class=\"knsl-name-and-photo\">\n" +
                "\t\t\t\t<div class=\"knsl-avatar-frame\">\n" +
                "\t\t\t\t\t<img src=\"" + newComment.getAccount().getImg() + "\" alt=\"Avatar\">\n" +
                "\t\t\t\t</div>\n" +
                "\t\t\t\t<div>\n" +
                "\t\t\t\t\t<h5>" + newComment.getAccount().getName() + "</h5>\n" +
                "\t\t\t\t\t<span>" + newComment.getTime() + "</span>\n" +
                "\t\t\t\t</div>\n" +
                "\t\t\t</div>\n" +
                "\t\t</div>\n" +
                "\t\t<blockquote>\n" +
                "\t\t   " + newComment.getContent() + "\n" +
                "\t\t</blockquote>\n" +
                "\t</div>\n";

//        result.put("result", temp);
//        return JSON.toJSONString(result);
        return temp;

    }


}





